package cn.hdufocus.focus_helper.controller;

import cn.hdufocus.focus_helper.annotation.MonitorRequest;
import cn.hdufocus.focus_helper.bean.MailBean;
import cn.hdufocus.focus_helper.bean.ResponseBean;
import cn.hdufocus.focus_helper.entity.Reservation;
import cn.hdufocus.focus_helper.exception.ForbiddenException;
import cn.hdufocus.focus_helper.service.IMailService;
import cn.hdufocus.focus_helper.service.IReservationService;
import cn.hdufocus.focus_helper.service.impl.MailServiceImpl;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reservation")
public class ReservationController {

    @Autowired
    private IReservationService reservationService;
    @Autowired
    private IMailService mailService;
    @Autowired
    MailBean mailBean;

    @RequiresAuthentication
    @GetMapping
    public ResponseBean listAll() {
        List<Reservation> reservations = reservationService.listAll();
        return new ResponseBean(200, "success", reservations);
    }

    @RequiresAuthentication
    @GetMapping("/unfinished")
    public ResponseBean listAllUnfinished() {
        List<Reservation> reservations = reservationService.listByStatusBetween(0, 2);
        return new ResponseBean(200, "success", reservations);
    }

    @RequiresAuthentication
    @GetMapping("/status")
    public ResponseBean listByStatus(@RequestParam int status) {
        List<Reservation> reservations = reservationService.listByStatus(status);
        return new ResponseBean(200, "success", reservations);
    }

    @RequiresAuthentication
    @GetMapping("/updateStatus")
    public ResponseBean updateStatus(@RequestParam int id, int status) {
        boolean flag = reservationService.updateStatus(id, status);
        if (flag) {
            return new ResponseBean(200, "success", null);
        } else {
            throw new ForbiddenException();
        }
    }

    @RequiresAuthentication
    @GetMapping("/updateLinkman")
    public ResponseBean updateLinkman(@RequestParam int id, String linkman) {
        boolean flag = reservationService.updateLinkman(id, linkman);
        if (flag) {
            return new ResponseBean(200, "success", null);
        } else {
            throw new ForbiddenException();
        }
    }

    @MonitorRequest
    @PostMapping
    public ResponseBean save(@RequestBody Reservation reservation) {
        Reservation reservation1 = reservationService.save(reservation);
        mailBean.setSubject("Focus-helper：预约单提醒");
        mailBean.setContent(reservation1.toString());
        if (reservation1.getFile() == null) {
            mailService.sendSimpleMail(mailBean);
        } else {
            mailService.sendAttachmentsMail(mailBean, reservation1.getFile());
        }
        return new ResponseBean(200, "success", reservation1);
    }

    @GetMapping("/search")
    public ResponseBean findById(@RequestParam long id) {
        Reservation reservation = reservationService.findById(id);
        if (reservation != null) {
            return new ResponseBean(200, "success", reservation);
        } else {
            throw new ForbiddenException();
        }
    }

    @GetMapping("/phone")
    public ResponseBean findByPhone(@RequestParam String phone) {
        List<Reservation> reservations = reservationService.listByPhone(phone);
        return new ResponseBean(200, "success", reservations);
    }
}
